/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/


import request from '@/utils/request';
import { IPromiseApi, IPromiseError } from '@/types/type';
import { IScenesDetail, IIconList, IBackgroundList, ICategoryList } from '@/types/sceneOfficial';
import { paginationParams } from '@/utils/soa/utils';
import { ICategoryItem } from '@/types/sceneCategory';
import base from './base';

// 开发者场景分页列表
export async function getScenesList({ current, pageSize, name, deployStatus,seriesCode, modelCode, }: any) {
  const { data } = await request.get(`/${base.API}/ms-soa-scene/${base.VERSION}/scene/page`, {
    params: {
      ...paginationParams(current, pageSize),
      deployStatus,
      name,
      seriesCode,
      modelCode,
    },
  });

  return {
    list: data?.itemSize
      ? data?.dataList.map((item: ICategoryItem, index: number) => ({
          ...item,
          index: index + 1 + (current - 1) * pageSize,
        }))
      : [],
    total: data?.pageCount,
  };
}
// 车型车系下拉框
export async function getSeriesModelList() {
  return await request.get(`/${base.API}/ms-soa-vehicle/${base.VERSION}/admin/model/selected`);
}

// 场景详情
export async function getScenesDetail(sceneUuid: string) {
  return await request.get<IPromiseApi<IScenesDetail>>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/scene`,
    {
      params: {
        sceneUuid,
      },
    },
  );
}

// 场景删除
export async function removeScenes(sceneUuid: string) {
  return await request.delete(`/${base.API}/ms-soa-scene/${base.VERSION}/scene`, {
    params: {
      sceneUuid,
    },
  });
}

// 编辑
export async function editScene(data: any) {
  {
    return await request(`/${base.API}/ms-soa-scene/${base.VERSION}/scene`, {
      method: 'post',
      data,
    });
  }
}

// 场景图标数据
export async function getIcon() {
  return await request.get<IPromiseApi<IIconList>>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/icon`,
  );
}

// 场景背景图数据
export async function getBackground() {
  return await request.get<IPromiseApi<IBackgroundList>>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/background`,
  );
}

// 场景分类数据
export async function getSceneCategory(params: { seriesCode: string }) {
  return await request.get<IPromiseApi<ICategoryList>>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/category`,
    {
      params
    }
  );
}

// 开发者场景发布接口
export async function release({sceneUuid,releaseType,cronReleaseTime} :{sceneUuid:string, releaseType?:any, cronReleaseTime?:any}) {
  return await request.put<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/scene/release`,
    {
      data: {
        sceneUuid,
        releaseType,
        cronReleaseTime,
      },
    },
  );
}

// 发布历史数据
// export async function getScenesList1<T = any>(params: any) {
//   const { data, pageable } = await request.get(`/${base.API}/ms-soa-scene-community/${base.VERSION}/scenesRecord/page`, {
//     params,
//   });
//   return {
//     list: data,
//     total: pageable!?.total,
//   };
// }
export async function getScenesList1({ current, pageSize, sceneUuid }: any) {
  const { data } = await request.get(
    `/${base.API}/ms-soa-scene-community/${base.VERSION}/scenesRecord/page`,
    {
      params: {
        ...paginationParams(current, pageSize),
        sceneUuid,
      },
    },
  );
  return {
    list: data?.itemSize
      ? data?.dataList.map((item: ICategoryItem, index: number) => ({
          ...item,
          index: index + 1 + (current - 1) * pageSize,
        }))
      : [],
    total: data?.pageCount,
  };
}
//发布历史详情
export async function releaseHistoryDetail(sceneRecordUuid: string) {
  return await request.get(`/${base.API}/ms-soa-scene-community/${base.VERSION}/scenesRecord`, {
    params: {
      sceneRecordUuid,
    },
  });
}
//下架
// export async function downService(data: any) {
//   const { sceneRecordUuid } = data;
//   return await request.post(`/${base.API}/ms-soa-scene-community/${base.VERSION}/scenes/soldout/${sceneRecordUuid}`, {
//   });
// }
export async function downService(data: any) {
  return await request.post(`/${base.API}/ms-soa-scene-community/${base.VERSION}/scenes/soldout`, {
    data,
  });
}

//发布审核
export async function checkAudit(sceneUuid: any) {
  return await request.get(
    `/${base.API}/ms-soa-scene-community/${base.VERSION}/scenes/checkAudit`,
    {
      params: {
        sceneUuid,
      },
    },
  );
}
